package com.spring.service.impl;

import com.spring.domain.Account;
import com.spring.mapper.AccountMapper;
import com.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    @Transactional(readOnly = true)
    public Account findById(Integer id) {
        System.out.println("AccountService.ById()");
       return accountMapper.selectAccountById(id);
//        return accountMapper.selectAccountById(8);
    }

    @Override
    @Transactional
    public void transfer(Integer fromId, Integer toId, Double money) {
        System.out.println("AccountServiceImpl.transfer");
        //查询两人的id
        Account from= accountMapper.selectAccountById(fromId);
        Account to = accountMapper.selectAccountById(toId);

        //借钱
      from.setMoney(from.getMoney()-money);
      to.setMoney(to.getMoney()+money);

      //更新账户
        accountMapper.updateMoneyById(from);
        accountMapper.updateMoneyById(to);


    }
}
